package com.wss.module.user.ui.account;

import com.wss.common.base.BaseActionBarAbility;
import com.wss.common.widget.ObserverButton;

import com.wss.module.user.ResourceTable;
import com.wss.module.user.ui.account.mvp.RegisterPresenter;
import com.wss.module.user.ui.account.mvp.contract.RegisterContract;

import butterknife.OnClick;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;

/**
 * Describe：注册
 * Created by 吴天强 on 2018/11/13.
 */

public class RegisterActivity extends BaseActionBarAbility<RegisterPresenter>
        implements RegisterContract.View {

    TextField edtName;
    TextField edtPwd;
    TextField edtConfirm;
    ObserverButton obRegister;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        initView();
    }

    @Override
    protected int getLayoutId() {
        return ResourceTable.Layout_user_layout_register;
    }

    protected void initView() {
        setCenterText("注册");

        edtName = (TextField) findComponentById(ResourceTable.Id_edt_name);
        edtPwd = (TextField) findComponentById(ResourceTable.Id_edt_pwd);
        edtConfirm = (TextField) findComponentById(ResourceTable.Id_edt_confirm);
        obRegister = (ObserverButton) findComponentById(ResourceTable.Id_ob_register);

        obRegister.observer(edtName, edtPwd, edtConfirm);

        obRegister.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {

                clearFocus(edtConfirm);

                getPresenter().register(edtName.getText().toString().trim(),
                        edtPwd.getText().toString().trim(),
                        edtConfirm.getText().toString().trim());
            }
        });
    }


    @Override
    protected RegisterPresenter createPresenter() {
        return new RegisterPresenter();
    }

    //clearFocus等同于隐藏软键盘
    public static void clearFocus(Component component){
        Component focusedComponent = findFocus(component, Component.FOCUS_NEXT);//尝试向后找焦点控件
        if (focusedComponent != null) {
            focusedComponent.clearFocus();
            return;
        }

        focusedComponent = findFocus(component, Component.FOCUS_PREVIOUS);//尝试向前找焦点控件
        if (focusedComponent != null) {
            focusedComponent.clearFocus();
        }
    }

    //找焦点控件
    public static Component findFocus(Component component, int direction){
        if (component.hasFocus()) {
            return component;
        }
        Component focusableComponent = component;
        int i = 99;
        while (i-->0){
            focusableComponent = focusableComponent.findNextFocusableComponent(direction);
            if (focusableComponent != null) {
                if (focusableComponent.hasFocus()) {
                    return focusableComponent;
                }
            } else {
                break;
            }
        }
        return null;
    }
}